<template>
  <div>
    <van-nav-bar
      :title="`${this.$route.params.key}` + ' 的搜索结果'"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          :to="`/article/${item.art_id}`"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { searchAPI } from '@/api'
export default {
  name: 'myResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      page: 1
    }
  },
  methods: {
    onClickLeft () {
      this.$router.back()
    },
    async onLoad () {
      const res = await searchAPI({
        page: this.page,
        q: this.$route.params.key
      })
      console.log('res', res)
      this.page++
      this.list.push(...res.data.data.results)
      this.loading = false
      if (this.list.length == res.data.data.total_count) {
        this.finished = true
      }
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false
      //   this.list = []
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
      this.refreshing = false
    }
  }
}
</script>

<style lang="less" scoped></style>
